package com.time.hellotime.utils;

import android.content.Context;
import android.content.Intent;

import com.time.hellotime.R;
import com.time.hellotime.app.AppConfig;
import com.yanzhenjie.nohttp.Headers;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.download.DownloadListener;
import com.yanzhenjie.nohttp.download.DownloadQueue;
import com.yanzhenjie.nohttp.download.DownloadRequest;
import com.zhy.base.fileprovider.FileProvider7;

import java.io.File;

/**
 * 下载apk
 * Created by Bing on 2018/1/4.
 * E-mail is tx16bing@163.com
 */

public class DownloadUtils {

    private DownloadRequest mDownloadRequest;

    private volatile static DownloadUtils mInstance;
    private DownloadQueue mQueue;

    private DownloadUtils() {
        // 私有化构造函数
        // 保证只有一个Queue对象
        this.mQueue = NoHttp.newDownloadQueue(3);
    }

    public static DownloadUtils getInstance() {
        if (mInstance == null) {
            synchronized (DownloadUtils.class) {
                if (mInstance == null) {
                    mInstance = new DownloadUtils();
                }
            }
        }
        return mInstance;
    }

    /**
     * 下载apk
     *
     * @param apkPath
     * @return
     */
    public void downLoadApk(final Context context, final String apkPath) {

//        String fileName = "time_person.apk";
//        final File file = new File(AppConfig.getInstance().APP_PATH_ROOT + File.separator + fileName);
        if (mDownloadRequest != null && mDownloadRequest.isStarted() && !mDownloadRequest.isFinished()) {
            // 暂停下载
            mDownloadRequest.cancel();
        } else if (mDownloadRequest == null || mDownloadRequest.isFinished()) {
            // 重新下载
            mDownloadRequest = new DownloadRequest(apkPath, RequestMethod.GET,
                    AppConfig.getInstance().APP_PATH_ROOT, true, true);
            mQueue.add(2, mDownloadRequest, new DownloadListener() {
                // 下载出现异常
                @Override
                public void onDownloadError(int what, Exception exception) {
                    Logger.e("Download exception " + exception.getMessage());
                }

                // 开始下载
                @Override
                public void onStart(int what, boolean isResume, long rangeSize,
                                    Headers responseHeaders, long allCount) {
                    Logger.e(context.getString(R.string.download_ing));
                }

                // 下载进度变化
                @Override
                public void onProgress(int what, int progress, long fileCount, long speed) {
                    //显示下载进度条
                    Logger.e("Download progress " + progress);
                }

                // 下载完成
                @Override
                public void onFinish(int what, String filePath) {
                    Logger.e("Download finish, file path: " + filePath);
                    File file = new File(filePath);
                    Intent intent = new Intent();
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setAction(android.content.Intent.ACTION_VIEW);
                    FileProvider7.setIntentDataAndType(
                            context, intent,
                            "application/vnd.android.package-archive",
                            file, true);
                    context.startActivity(intent);
                }

                // 下载取消或者终止
                @Override
                public void onCancel(int what) {
//                    Logger.e("Download cancel");
                }
            });
        }
    }

}
